package com.yami.shop.rights.multishop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.rights.common.dto.ProdRightsDto;
import com.yami.shop.rights.common.dto.RightsVerificationUserDto;
import com.yami.shop.rights.common.model.ProdRights;
import com.yami.shop.rights.common.model.ProdRightsExternalData;
import com.yami.shop.rights.common.model.RightsVerificationUser;
import com.yami.shop.rights.common.service.RightsVerificationUserService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/p/rightsVerificationUser")
@Tag(name = "权益核销管理员（商户端）")
@Slf4j
public class RightsVerificationUserController {

    @Autowired
    private RightsVerificationUserService rightsVerificationUserService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    @Operation(summary = "权益核销管理员列表")
    public ServerResponseEntity<IPage<RightsVerificationUser>> pageList(RightsVerificationUserDto vo, PageParam<RightsVerificationUser> page) {
        vo.setShopId(SecurityUtils.getShopUser().getShopId());
        IPage<RightsVerificationUser> pageData = rightsVerificationUserService.pageList(page, vo);
        return ServerResponseEntity.success(pageData);
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "权益核销管理员新增/修改")
    public ServerResponseEntity<Void> saveOrUpdate(@RequestBody RightsVerificationUser rightsVerificationUser){
        // 判断手机号是否实名
        User userDetailByCallPhone = userService.getUserDetailByCallPhone(rightsVerificationUser.getCallPhone());
        if(ObjectUtil.isEmpty(userDetailByCallPhone)){
            return ServerResponseEntity.showFailMsg("该手机号不存在");
        }
        if(ObjectUtil.isEmpty(userDetailByCallPhone.getIdNumber())){
            return ServerResponseEntity.showFailMsg("该手机号未实名");
        }
        if(!ObjectUtil.equal(rightsVerificationUser.getName(),userDetailByCallPhone.getRealName())){
            return ServerResponseEntity.showFailMsg("手机号与名称不匹配");
        }
        // 输入名字与注册用户是否相同
        rightsVerificationUser.setUserId(userDetailByCallPhone.getUserId());
        rightsVerificationUser.setShopId(SecurityUtils.getShopUser().getShopId());
        boolean saveOrUpdate = rightsVerificationUserService.saveOrUpdate(rightsVerificationUser);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除权益核销管理员")
    public ServerResponseEntity<Void> deleteBrand(@RequestParam Long id) {
        // 删除权益核销管理员
        UpdateWrapper<RightsVerificationUser> prodRightUpdate = new UpdateWrapper<>();
        prodRightUpdate.eq("id", id);
        prodRightUpdate.ne("status", -1);
        prodRightUpdate.set("status", -1);
        rightsVerificationUserService.update(prodRightUpdate);
        return ServerResponseEntity.success();
    }

    @GetMapping("/updateStatus")
    @Operation(summary = "修改权益核销管理员状态")
    public ServerResponseEntity<Void> updateStatus(@RequestParam Long id,@RequestParam Long status) {
        // 修改权益核销管理员状态
        UpdateWrapper<RightsVerificationUser> prodRightUpdate = new UpdateWrapper<>();
        prodRightUpdate.eq("id", id);
        prodRightUpdate.ne("status", "-1");
        prodRightUpdate.set("status", status);
        rightsVerificationUserService.update(prodRightUpdate);
        return ServerResponseEntity.success();
    }
}
